package top.rushpeak.edu03.admin.web;


import java.sql.SQLException;

import org.quartz.Job;

import com.alibaba.druid.util.StringUtils;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.render.JsonRender;

import top.rushpeak.edu03.admin.util.QuartzManager;
import top.rushpeak.edu03.admin.util.Result;

public class JobmanageController extends BaseController {

	public void index() {
		
		render("index.html");
	}
	
	public void post(){
		System.out.println("post");
		int pageNum = getParaToInt("page", 1);
		int rows = getParaToInt("rows", 30);
		String whereSql = " where 1=1 ";

		pageNum = pageNum > 0 ? pageNum : 1;
		Page<Record> page = Db.paginate(pageNum, rows, "select * ",
			" from  job_manager" + whereSql + "order by id desc");
		setAttrs(buildPagination(page.getList(), page.getTotalRow()));
		render(new JsonRender().forIE());
		
	}
	
	
	public void add(){
		System.out.println("add");
		String id = getPara("id");
		String name = getPara("name");
		String group = getPara("group");
		String clazz = getPara("clazz");
		String cron_expression = getPara("cron_expression");
		String is_enabled = getPara("is_enabled");
		try {
			Class<? extends Job>  jobClazz = Class.forName(clazz).asSubclass(Job.class);
			
			//插入数据库
			Record r = new Record();
			r.set("name", name);
			r.set("group", group);
			r.set("clazz", clazz);
			r.set("cron_expression", cron_expression);
			r.set("is_enabled", "N");
			
    		if(StringUtils.isEmpty(id)){
    			Db.save("job_manager", "id",r);
    			
    		}else{
    			r.set("id", id);
    			Db.update("job_manager", "id",r);
    		}
			
		} catch (Exception e) {
			LOG.error("e=="+e);
			System.out.println("e=="+e);
			renderJson(Result.error("异常"));
		}
		
		renderJson(Result.OK);
	}
	
	//启用关停
	public void delete(){
		
		try {
			final String id = getPara("id");
			final Record r = Db.findById("job_manager", id);
			System.out.println(r);
			if(r!=null){
				
		        boolean bl = Db.tx(new IAtom() {  
		            @Override  
		            public boolean run() throws SQLException {  
						String name = r.getStr("name");
						String group = r.getStr("group");
						String clazz = r.getStr("clazz");
						String cron_expression = r.getStr("cron_expression");
						String is_enabled = r.getStr("is_enabled");
						String change_enable = "N".equals(is_enabled)?"Y":"N";
						r.set("is_enabled", change_enable);
						Db.update("job_manager", "id", r);
		        		System.out.println("db.updat:"+change_enable);
		        		Class<? extends Job> jobClazz;
						try {
							jobClazz = Class.forName(clazz).asSubclass(Job.class);
						} catch (ClassNotFoundException e) {
							System.out.println("job类异常");
							throw new RuntimeException("job类异常");
						}
		        		
		        		QuartzManager qm = new QuartzManager();
		        		if("Y".equals(r.getStr("is_enabled"))){
		        			try {
		        				qm.removeJob(r.getStr("name"), r.getStr("group"));								
							} catch (Exception e) {
								System.out.println("removeJob_err:"+e);
							}
		        			qm.addJob(r.getStr("name"), r.getStr("group"), jobClazz, r.getStr("cron_expression"));
		        		}else{
		        			qm.removeJob(r.getStr("name"), r.getStr("group"));
		        		}
		                 
		        		return true;
		            }  
		        }); 
				
			}
			
		} catch (Exception e) {
			LOG.error("e=="+e);
			System.out.println("e=="+e);
			renderJson(Result.error("异常"));
		}
		
		renderJson(Result.OK);
	}
	
	public void get(){
		System.out.println("get");
		Record r = null;
		try {
			String id = getPara("id");
			r = Db.findById("job_manager", id);
			
		} catch (Exception e) {
			LOG.error("e=="+e);
			System.out.println("e=="+e);
			renderJson(Result.error("异常"));
		}
		
		renderJson(Result.success(r));
	}

	
	

	
	
}
